<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\PrfCoupon;

/**
 * PrfCouponSearch represents the model behind the search form about `backend\models\PrfCoupon`.
 */
class PrfCouponSearch extends PrfCoupon
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'img_id', 'action_id', 'start_time', 'end_time', 'totle_count', 'last_count', 'user_max_number', 'state', 'time_state', 'shipping'], 'integer'],
            [['name', 'title', 'memo', 'action', 'url', 'coupon_rule', 'user_types', 'excode'], 'safe'],
            [['coupon_price', 'price'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = PrfCoupon::find()->orderBy("id desc ");

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'img_id' => $this->img_id,
            'action_id' => $this->action_id,
            'start_time' => $this->start_time,
            'end_time' => $this->end_time,
            'coupon_price' => $this->coupon_price,
            'price' => $this->price,
            'totle_count' => $this->totle_count,
            'last_count' => $this->last_count,
            'user_max_number' => $this->user_max_number,
            'state' => $this->state,
            'time_state' => $this->time_state,
            'shipping' => $this->shipping,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'memo', $this->memo])
            ->andFilterWhere(['like', 'action', $this->action])
            ->andFilterWhere(['like', 'url', $this->url])
            ->andFilterWhere(['like', 'coupon_rule', $this->coupon_rule])
            ->andFilterWhere(['like', 'user_types', $this->user_types])
            ->andFilterWhere(['like', 'excode', $this->excode]);

        return $dataProvider;
    }
}
